#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>

//851
using namespace std;

const int N = 1e5 + 10;

int n, m;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
bool state[N];

void add(int a, int b, int c){
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

void spfa(){
    memset(dist, 0x3f, sizeof dist);
    queue<int> que;
    dist[1] = 0;
    que.push(1);
    state[1] = true;
    while(que.size()){
        int k = que.front();
        que.pop();

        state[k] = false;

        for (int u = h[k]; u != -1; u = ne[u]){
            int j = e[u];
            if(dist[j] > dist[k] + w[u]){
                dist[j] = dist[k] + w[u];
                if(!state[j]){
                    que.push(j);
                    state[j] = true;
                }
            }
        }
    }

    if(dist[n] == 0x3f3f3f3f){
        cout << "impossible" << endl;
    }else{
        cout << dist[n] << endl;
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 0; i < m; i ++){
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }

    spfa();

   
    return 0;
}